import urllib.request
from lxml import etree

def create_request(page):
#     https://sc.chinaz.com/tupian/qinglvtupian.html
#     https://sc.chinaz.com/tupian/qinglvtupian_2.html

    if(page == 1):
        url = 'https://sc.chinaz.com/tupian/qinglvtupian.html'
    else:
        url = 'https://sc.chinaz.com/tupian/qinglvtupian_'+str(page)+'.html'
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0'}
    request = urllib.request.Request(url = url,headers = headers)
    return request

def create_content(request):
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    # print(content)
    return content

def down_load(content):
    tree = etree.HTML(content)
    alt_name = tree.xpath('//div[@class="item"]/img/@alt')
    # for name in alt_name :
    #     print(name)
    src_name = tree.xpath('//div[@class="item"]/img/@data-original')
    # for name in src_name:
    #     print(name)
    #     http_src_name = 'https:'+name
    #     print(http_src_name)
    for i in range(len(alt_name)):
        alt = alt_name[i]
        src = src_name[i]
        # print(alt,'https:'+src)

        urllib.request.urlretrieve(url= 'https:'+src,filename='../下载的图片/'+alt+'.jpg')






if __name__ == '__main__':
   start_page =  int(input('请输入起始页码:'))
   end_page = int(input('请输入结束页码:'))

   for page in range(start_page,end_page+1):
       request = create_request(page)
       content = create_content(request)
       down_load(content)

